from odoo import api, fields, models


class RpInviteSchedule(models.Model):
    _name = 'rp.invite.schedule'
    _description = '邀请任务'
    _sql_constraints = [('invite_code', 'UNIQUE(rp_reward_incentive_rule_id,invite_code)', '邀请码和邀请规则需要唯一')]
    _rec_name = 'code'

    code = fields.Char(string='序列号', default='RI_SCH/****', index=True)
    rp_customer_id = fields.Many2one(string='关联主表', comodel_name='rp.customer', copy=False)
    rp_reward_incentive_rule_id = fields.Many2one(string='奖励规则', comodel_name='rp.reward.incentive.rule', copy=False)
    invite_code = fields.Char(string='邀请码', copy=False)
    invite_link = fields.Char(string='邀请链接', copy=False)
    invite_number = fields.Integer(string='邀请人数', copy=False, default=0)
    invite_category_id = fields.Many2one(string='邀请类型', comodel_name='rp.invite.category', copy=False)
    date_create = fields.Datetime(string='创建时间', default=fields.datetime.now(), copy=False)
    validate_flag = fields.Boolean(string='有效', default=True, copy=False)

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            vals['code'] = self.env['ir.sequence'].sudo().next_by_code('rp.invite.schedule') or '/'
        return super(RpInviteSchedule, self).create(vals_list)


class RpInviteCategory(models.Model):
    _name = 'rp.invite.category'
    _description = '邀请类型'
    _sql_constraints = [('name', 'UNIQUE(name)', '名称必须唯一'), ('invite_code_prefix', 'UNIQUE (invite_code_prefix)', '邀请码前缀必须唯一')]

    name = fields.Char(string='名称', copy=False)
    invite_code_prefix = fields.Char(string='邀请码前缀', copy=False)
    desc = fields.Char(string='描述', copy=False)
